package hanjoapp.server;

import hanjoapp.client.RegistrationService;
import hanjoapp.client.User;
import hanjoapp.shared.FieldVerifier;

import com.google.gwt.user.server.rpc.RemoteServiceServlet;
import com.googlecode.objectify.Objectify;
import com.googlecode.objectify.ObjectifyService;

/**
 * The server side implementation of the RPC service.
 */
@SuppressWarnings("serial")
public class RegistrationServiceImpl extends RemoteServiceServlet implements
		RegistrationService {

	
	public User registrationServer(String login, String pwd, String pwdConfirm, String email,
			String firstName, String lastName) throws IllegalArgumentException {
		
		// Verify that the input is valid. 
		/* if (FieldVerifier.loginAvailable(login)) {
			// If the input is not valid, throw an IllegalArgumentException back to
			// the client.
			throw new IllegalArgumentException(
					"This login name is already taken");
		} */
		if (FieldVerifier.isValidPassword(pwd, pwdConfirm)) {
			// If the input is not valid, throw an IllegalArgumentException back to
			// the client.
			throw new IllegalArgumentException(
					"Your passwords do not match");
		}
		
		ObjectifyService.register(User.class);
        Objectify ofy = ObjectifyService.begin();

        User user = new User(login, pwd, email, firstName, lastName);
        //Use setters to populate the object
        //the Id will be auto generated and does not need to be set
        
        ofy.put(user);
        assert user.getId() != null;    // id was autogenerated
        user = ofy.query(User.class).filter("login", login).get();
 		return user;
 	}
}
